package com.khiker.linklist;

public class P19_RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) {
            return head;
        }
        int i = 0;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null) {
            fast = fast.next;
            if (i <= n) {
                i++;
            } else {
                slow = slow.next;
            }
        }
        if (i == n) {
            head = slow.next;
            return head;
        }
        slow.next = slow.next.next;
        return head;
    }
}
